package com.csjbot.weedle_auto.view

import android.app.Activity
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup.LayoutParams
import android.widget.Button
import android.widget.LinearLayout
import android.widget.PopupWindow

import com.csjbot.weedle_auto.R

@Suppress("DEPRECATION")
/**
 * Created by chenqi on 8/9/17.
 */

class SelectPicPopupWindow(context: Activity, itemsOnClick: View.OnClickListener) : PopupWindow(context) {

    private val email: LinearLayout
    private val save_folder: LinearLayout
    private val cancel_popup: Button
    private val mMenuView: View

    init {
        val inflater = context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        mMenuView = inflater.inflate(R.layout.popup_dialog, null)
        email = mMenuView.findViewById(R.id.email_layout) as LinearLayout
        save_folder = mMenuView.findViewById(R.id.folder_layout) as LinearLayout
        cancel_popup = mMenuView.findViewById(R.id.cancel_popup) as Button
        //取消按钮
        cancel_popup.setOnClickListener {
            //销毁弹出框
            dismiss()
        }
        //设置按钮监听
        email.setOnClickListener(itemsOnClick)
        save_folder.setOnClickListener(itemsOnClick)
        //设置SelectPicPopupWindow的View
        this.contentView = mMenuView
        //设置SelectPicPopupWindow弹出窗体的宽
        this.width = LayoutParams.FILL_PARENT
        //设置SelectPicPopupWindow弹出窗体的高
        this.height = LayoutParams.WRAP_CONTENT
        //设置SelectPicPopupWindow弹出窗体可点击
        this.isFocusable = true
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.animationStyle = R.style.AnimBottom
        //实例化一个ColorDrawable颜色为半透明
        val dw = ColorDrawable(0xb0000000.toInt())
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw)
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener { _, event ->
            val height = mMenuView.findViewById(R.id.pop_layout).top
            val y = event.y.toInt()
            if (event.action == MotionEvent.ACTION_UP) {
                if (y < height) {
                    dismiss()
                }
            }
            true
        }

    }
}
